package com.cy.ppj.web.admin.order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.dto.manager.ManagerLoginDTO;
import com.cy.ppj.model.enums.ManagerType;
import com.cy.ppj.model.enums.OrderStatus;
import com.cy.ppj.model.query.order.OrderScanRecordAdminQuery;
import com.cy.ppj.model.vo.admin.order.OrderScanRecordAdminVO;
import com.cy.ppj.service.order.OrderService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-订单浏览记录
 * @author: tanyilin
 * @create: 2018年8月16日 上午10:37:44
 */
@Slf4j
@RestController
@RequestMapping("/admin/order/scan/record")
@Api(tags = "后台-订单浏览记录")
public class AdminOrderScanRecordController extends BaseController{

	@Autowired
	private OrderService orderService;
	
	@ApiOperation("待确认列表")
	@GetMapping("/unconfirm/list")
	public PageBean<OrderScanRecordAdminVO> unConfirmList(OrderScanRecordAdminQuery query) {
		// 当前登录的管理员账户
		ManagerLoginDTO manager = this.getManager();
		PageBean<OrderScanRecordAdminVO> pageBean = new PageBean<>(null, 0);
		query.setType(1);
		if(ManagerType.GENERAL_MANAGER.equals(manager.getType())) {
			// 综合管理员
			pageBean = orderService.selectOrderScanRecordAdmin(query);
			
		}else if(ManagerType.CITY_LEADER.equals(manager.getType())) {
			// 城市负责人
			query.setCityId(manager.getCityId());
			pageBean = orderService.selectOrderScanRecordAdmin(query);
			
		}else if(ManagerType.COMPANY_LEADER.equals(manager.getType())) {
			// 渠道公司负责人
			query.setCompanyId(manager.getCompanyId());
			pageBean = orderService.selectOrderScanRecordAdmin(query);
		}
		return pageBean;
	}
	
	@ApiOperation("已退回列表")
	@GetMapping("/return/list")
	public PageBean<OrderScanRecordAdminVO> returnList(OrderScanRecordAdminQuery query) {
		// 当前登录的管理员账户
		ManagerLoginDTO manager = this.getManager();
		PageBean<OrderScanRecordAdminVO> pageBean = new PageBean<>(null, 0);
		query.setType(2);
		if(ManagerType.GENERAL_MANAGER.equals(manager.getType())) {
			// 综合管理员
			pageBean = orderService.selectOrderScanRecordAdmin(query);
			
		}else if(ManagerType.CITY_LEADER.equals(manager.getType())) {
			// 城市负责人
			query.setCityId(manager.getCityId());
			pageBean = orderService.selectOrderScanRecordAdmin(query);
			
		}else if(ManagerType.COMPANY_LEADER.equals(manager.getType())) {
			// 渠道公司负责人
			query.setCompanyId(manager.getCompanyId());
			pageBean = orderService.selectOrderScanRecordAdmin(query);
		}
		return pageBean;
	}
	
	@ApiOperation("确认订单")
	@PostMapping("/do/confirm")
	public Result doConfirm(@ApiParam(value = "订单ID", example = "0", required = true) @RequestParam Long orderId) {
		orderService.doConfirmOrReturnOrderScanRecord(orderId, OrderStatus.APPLY_SUCCESS, this.getManager().getManagerId());
		return ok();
	}
	
	@ApiOperation("退回订单")
	@PostMapping("/do/return")
	public Result doReturn(@ApiParam(value = "订单ID", example = "0", required = true) @RequestParam Long orderId) {
		orderService.doConfirmOrReturnOrderScanRecord(orderId, OrderStatus.APPLY_FAIL, this.getManager().getManagerId());
		return ok();
	}
}
